package com.example.trb.service.review;

import com.example.trb.dto.ObjectDTO;
import com.example.trb.entity.*;
import com.example.trb.mapper.*;
import com.example.trb.service.education.EducationService;
import com.example.trb.service.paper.PaperService;
import com.example.trb.service.performance.PerformanceAwardService;
import com.example.trb.service.performance.PerformancePatentService;
import com.example.trb.service.performance.PerformanceResultService;
import com.example.trb.service.user.TimService;
import com.example.trb.service.work.WorkExperienceService;
import com.example.trb.util.DateFormatUtil;
import com.example.trb.util.result.ReviewFormCode;
import com.example.trb.util.result.SysReturnCode;
import com.example.trb.vo.InitialFormVO;
import com.example.trb.vo.ReviewFormVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReviewFormService {
    @Autowired
    DeclarationSeriesMapper dsMapper;
    @Autowired
    ReviewMeetingMapper rmMapper;
    @Autowired
    ReviewFormMapper rfMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    EducationService educationService;
    @Autowired
    PaperService paperService;
    @Autowired
    PerformanceResultService prService;
    @Autowired
    PerformanceAwardService paService;
    @Autowired
    PerformancePatentService ppService;
    @Autowired
    WorkExperienceService weService;
    @Autowired
    TimService timService;
    @Autowired
    SpecialtyMapper specialtyMapper;

    public List<Specialty> listSpecialty(String series) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("declaration_series", series);
        List<Specialty> result = specialtyMapper.selectByMap(queryMap);
        return result;
    }

    public List<ReviewForm> listReviewForm(Map<String, Object> queryMap) {
        return rfMapper.selectByMap(queryMap)
                .stream()
                .map(reviewForm -> {
                    reviewForm.setReviewYear(
                            DateFormatUtil.dateStrToYear(reviewForm.getReviewYear())
                    );
                    return reviewForm;
                })
                .collect(Collectors.toList());
    }

    public ObjectDTO updateStatusById(Integer id, String status) {
        rfMapper.updateStatusById(id, status);
        return new ObjectDTO(null, SysReturnCode.OK);
    }

    public ObjectDTO deleteById(Integer id) {
        rfMapper.deleteById(id);
        return new ObjectDTO(null, SysReturnCode.OK);
    }

    public ObjectDTO updateById(ReviewForm data) {
        rfMapper.updateById(data);
        return new ObjectDTO(null, SysReturnCode.OK);
    }

    public ReviewFormVO getReviewFormById(Integer id) {
        ReviewFormVO vo = new ReviewFormVO();
        Map<String, Object> queryMap = new HashMap<>();

        // 先获取评审表的基本信息
        ReviewForm reviewForm = rfMapper.selectById(id);
        vo.setReviewForm(reviewForm);

        // 获取用户信息
        Integer userId = reviewForm.getUserId();
        queryMap.put("user_id", userId);
        List<UserInfo> userAllInfos = userInfoMapper.selectByMap(queryMap);
        if (!userAllInfos.isEmpty())
            vo.setUserAllInfo(userAllInfos.get(0));
        else {
            UserInfo userInfo = new UserInfo();
            vo.setUserAllInfo(userInfo);
        }


        // 获取学历信息
        List<Education> educations = (List<Education>) educationService.listByUserId(userId).getData();
        vo.setEducation(educations);

        // 获取工作经历
        List<WorkExperience> experiences = (List<WorkExperience>) weService.listByUserId(userId).getData();
        vo.setWorkExperience(experiences);

        // 获取论文
        List<Paper> papers = paperService.listByReviewFormId(id);
        vo.setPaper(papers);

        // 获取业绩成果
        List<PerformanceResult> prs = prService.listByReviewFormId(id);
        vo.setPerformanceResult(prs);

        // 获取业绩奖励
        List<PerformanceAward> pas = paService.listByReviewFormId(id);
        vo.setPerformanceAward(pas);

        // 获取业绩专利
        List<PerformancePatent> pps = ppService.listByReviewFormId(id);
        vo.setPerformancePatent(pps);

        // 获取人才引进材料
        List<TalentIntroductionMaterial> tims = timService.listByReviewFormId(id);
        vo.setTalentIntroductionMaterial(tims);

        return vo;
    }

    public ObjectDTO listReviewForm(Integer userId) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("user_id", userId);
        List<ReviewForm> result = rfMapper.selectByMap(queryMap)
                .stream()
                .map(reviewForm -> {
                    reviewForm.setReviewYear(
                            DateFormatUtil.dateStrToYear(reviewForm.getReviewYear())
                    );
                    return reviewForm;
                })
                .collect(Collectors.toList());
        return new ObjectDTO(result, SysReturnCode.OK);
    }

    public ObjectDTO initialForm(InitialFormVO vo, Integer userId) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("level", vo.getLevel());
        String year = DateFormatUtil.nowYear();
        queryMap.put("review_year", year);
        List<ReviewForm> exist = rfMapper.selectByMap(queryMap);
        if (!exist.isEmpty()) {
            return new ObjectDTO(null, ReviewFormCode.NOW_YEAR_HAS_FORM);
        }

        ReviewForm form = new ReviewForm();
        form.setCreateTime(DateFormatUtil.dateToString(new Date()));
        form.setReviewYear(year);
        form.setUserId(userId);
        form.setDeclarationSeries(vo.getDeclarationSeries());
        form.setReviewMeetingId(vo.getReviewMeetingId());
        form.setLevel(vo.getLevel());
        rfMapper.insert(form);
        return new ObjectDTO(form.getId(), SysReturnCode.OK);
    }

    public List<String> listDecSeries() {
        return dsMapper.selectAll();
    }

    public List<ReviewMeeting> listReviewMeeting(String level, String series) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("level", level);
        queryMap.put("declaration_series", series);
        List<ReviewMeeting> result = rmMapper.selectByMap(queryMap);
        return result;
    }
}
